package com.chinatechstar.admin.service.impl;

import com.chinatechstar.admin.entity.Dept;
import com.chinatechstar.admin.entity.JsonObj;
import com.chinatechstar.admin.mapper.DeptInfoMapper;
import com.chinatechstar.admin.service.DeptInfoService;
import com.chinatechstar.admin.service.SysUserService;
import com.chinatechstar.admin.vo.DeptVo;
import com.chinatechstar.component.commons.result.PaginationBuilder;
import com.chinatechstar.component.commons.utils.CurrentUserUtils;
import com.chinatechstar.component.commons.utils.RecursiveListUtils;
import com.chinatechstar.component.commons.utils.UuidUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class DeptInfoServiceImpl implements DeptInfoService {

    @Autowired
    private DeptInfoMapper deptInfoMapper;

    @Autowired
    private SysUserService sysUserService;

    @Override
    public JsonObj add(Dept dept) {
        // 查当前登录人所属机构orgId
        String orgId=sysUserService.selectOrgIdByUserId(dept.getUserId());
        dept.setOrgId(orgId);
        dept.setId(UuidUtil.get32UUID());
        int count=deptInfoMapper.addDept(dept);
        if (count>0) {
            return JsonObj.success("新增成功");
        }else{
            return JsonObj.error("新增失败");
        }
    }

    @Override
    public JsonObj del(String id) {
        int count = deptInfoMapper.delDept(id);
        if (count>0) {
            return JsonObj.success("删除成功");
        }else{
            return JsonObj.error("删除失败");
        }
    }

    @Override
    public JsonObj edit(Dept dept) {
        int count = deptInfoMapper.updateDept(dept);
        if (count>0) {
            return JsonObj.success("修改成功");
        }else{
            return JsonObj.error("修改失败");
        }
    }

    @Override
    public Map<String, Object> selById(String id) {
        return deptInfoMapper.getDeptById(id);
    }

    @Override
    public Map<String, Object> queryDept(DeptVo deptVo) {
        Integer currentPage = deptVo.getCurrentPage();
        Integer pageSize = deptVo.getPageSize();
        Map<String, Object> paramMap = new HashMap<>();
        String userId = deptVo.getUserId();
        String orgId=sysUserService.selectOrgIdByUserId(userId);
        paramMap.put("orgId", orgId);
        Page<Object> page = PageHelper.startPage(currentPage, pageSize);
        List<LinkedHashMap<String, Object>> resultList =deptInfoMapper.queryDept(paramMap);
        return PaginationBuilder.buildResult(resultList,
                page.getTotal(),
                currentPage,
                pageSize);
    }

    @Override
    public LinkedHashMap<String, Object> getDeptList(String orgId) {
        LinkedHashMap<String, Object> resultMap = new LinkedHashMap<>();
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("orgId", orgId);
        List<LinkedHashMap<String, Object>> list = deptInfoMapper.queryDept(paramMap);
        resultMap.put("list", list);
        return resultMap;
    }
}
